<template>
    <div class="wrapper">
      <div style="margin: 100px auto; background-color: #fff; width: 350px; height: 400px; padding: 20px; border-radius: 10px">
        <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注 册</b></div>
        <el-form :model="user" :rules="rules" ref="userForm">
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" size="default" style="margin: 10px 0" prefix-icon="User" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" size="default" style="margin: 10px 0;" prefix-icon="Lock" show-password v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input placeholder="请确认密码" size="default" style="margin: 10px 0;" prefix-icon="Lock" show-password v-model="user.confirmPassword"></el-input>
          </el-form-item>
          <div style="margin: 10px 0; text-align: right">
            <el-button type="primary" size="default" autocomplete="off" @click="register(userForm)">注册</el-button>
            <el-button type="warning" size="default" autocomplete="off" @click="back">返回登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
 
        import {ref, reactive} from 'vue'
        import { useRouter } from 'vue-router'
        import instance from '@/utils/request'
        import { ElMessage } from 'element-plus';

        const router = useRouter()
        
        const user = ref({})
        const userForm = ref<FormInstance>()

        const rules =  {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
          ],
          confirmPassword: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
          ]
        }

        const back = () => {
           router.push('login')
        }

        const register = (formEl: FormInstance | undefined) => {
            if (!formEl) return
            formEl.validate(async (valid) => {
              if (valid) {
                if(user.value.password != user.value.confirmPassword){
                  ElMessage({
                    message: '两次密码输入不一致',
                    type: 'error'
                  })
                  return
                }
                const res = await instance.post('/admin/register', user.value)
                  if(res.data.code == '200'){
                     ElMessage({
                      message: '注册成功',
                      type: 'success'
                    })
                    router.push('login')
                  }else{
                    ElMessage({
                      message: res.data.msg,
                      type: 'error'
                    })
                  }
              } 
            })
          }
  
  </script>
  
  <style scoped>
    .wrapper{
      height: 100vh;
      background-image: linear-gradient(to bottom right, #FC466B, #3F5EFB);
      overflow: hidden;
    }
  </style>